
using System;
using System.Data;
using System.Globalization;
using System.IO;
using System.Windows.Forms;

namespace IPAMS.Views
{
    /// <summary>
    /// This is an editor for the Dividends table. It enables loading from a file,
    ///saving to a file and more
    /// </summary>
    public partial class DividendsEditor : UserControl
    {
        public DSIPAMS.DTDividendsDataTable dividends;
        string[] dividendsHeaders;

        /// <summary>
        /// Initializes a new instance of the <see cref="DividendsEditor"/> class.
        /// </summary>
        public DividendsEditor()
        {
            InitializeComponent();
            InitializeComponentExtended();
        }

        /// <summary>
        /// Initializes the component extended.
        /// </summary>
        private void InitializeComponentExtended()
        {   
            dividends = new DSIPAMS.DTDividendsDataTable();
        }

        /// <summary>
        /// Handles the DataSourceChanged event of the dgvDividends control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void dgvDividends_DataSourceChanged(object sender, EventArgs e)
        {
            dgvDividends.Refresh();
        }

        /// <summary>
        /// Loads the data from a file.
        /// </summary>
        /// <param name="dataFilePath">The data file path.</param>
        /// <returns>a DataTable with the file's data</returns>
        public DataTable LoadData(string dataFilePath)
        {

            if (File.Exists(dataFilePath))
            {
                StreamReader sr = new StreamReader(dataFilePath);
                dividendsHeaders = sr.ReadLine().Split(',');
                ;
                while (true)
                {
                    string dividend = sr.ReadLine();
                    if (dividend == string.Empty || dividend == null) break;
                    string[] dividendInfo = dividend.Split(',');
                    if (dividendInfo.Length != 4) continue;
                    CultureInfo ci = new CultureInfo("he-IL");
                 
                    dividends.AddDTDividendsRow(dividendInfo[0], dividendInfo[1], DateTime.Parse(dividendInfo[2],ci.DateTimeFormat),
                                                Convert.ToDouble(dividendInfo[3]));
                }
                sr.Close();


                Text = dataFilePath;

                dividends.AcceptChanges();
                dgvDividends.DataSource = dividends;
                for (int index = 0; index < dividendsHeaders.Length; index++)
                {
                    dgvDividends.Columns[index].HeaderText = dividendsHeaders[index];
                }
                foreach (DataGridViewColumn column in dgvDividends.Columns)
                {
                    column.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                }
            }


            return dividends;
        }

        /// <summary>
        /// Saves the data to a file.
        /// </summary>
        /// <param name="dataFilePath">The data file path.</param>
        public void SaveData(string dataFilePath)
        {
            StreamWriter sw = new StreamWriter(dataFilePath);
            string fileContent = dividendsHeaders + Environment.NewLine;
            foreach (DSIPAMS.DTDividendsRow dividend in dividends)
            {
                fileContent += string.Format("{0},{1},{2},{3}" + Environment.NewLine, dividend[0], dividend[1], dividend[2], dividend[3]);
            }
            sw.Write(fileContent);
            sw.Close();

        }
    }
}
